import {
  HomeOutlined,
  IdcardOutlined,
  MailOutlined,
  ProfileOutlined,
  SettingOutlined,
  ToolOutlined,
  TransactionOutlined,
  UserOutlined,
} from '@ant-design/icons';

import MyIcon from 'app/components/MyIcon'
import React from 'react';
import { MENU_CODES } from 'app/constants/menu';
import themeConfig from 'app/core/themeConfig'
/**
 * MenuAdditionalSettings 配置说明
 * 用于补充数据库中的菜单没有配置的字段，如icon，showInNav等
 * @param icon 菜单图标
 * @param titleIcon 标题图标，仅用于标题展示，若无，则标题自动加载菜单图标即icon字段
 * @param popupClassName 下拉菜单的className
 * @param  showInNav 是否在顶部主菜单中显示
 * @param 其他属性如menuName等将会覆盖后端返回的对应属性
 * @param  redirectUrl 父级菜单跳转路径，用于跳转到子菜单对应的路径
 * @param  temporarilyHide 临时隐藏，不出现在侧栏菜单，也会作为临时判断使用
*/
const MenuAdditionalSettings = {
  [MENU_CODES.OVERVIEW]: {
    icon: <HomeOutlined style={{ fontSize: '18px'}}></HomeOutlined>,
    showInNav: true,
    menuName: '总览'
  },
  [MENU_CODES.CLOUD]: {
    icon: <MyIcon type="icon-yunzhuji" style={{ fontSize: '18px'}}></MyIcon>,
    popupClassName: "header-menu-popup",
    showInNav: true,
    menuName: '云服务器',
    redirectUrl: "/cloud/compute"
  }
}

function findMatchingKey(currentRoute, Menus=[]) {
  for (const item of Menus) {
    if (item.url === currentRoute) {
          return item.menuCode
      }
      if (item.childList) {
          const result = findMatchingKey(currentRoute, item.childList);
          if (result) {
              return result;
          }
      }
  }
  return null;
}
function findMatchingItem(currentRoute, Menus=[]) {
  for (const item of Menus) {
      if (item.url === currentRoute) {
          return item;
      }
      if (item.childList) {
          for (const child of item.childList) {
              if (child.url === currentRoute) {
                  return child;
              }else {
                let next_child = findMatchingItem(currentRoute, child.childList)
                if(next_child != null) {
                  return next_child
                }
              }
          }
      }
  }
  return null;
}

// 判断current匹配的菜单是否位于前二层级以内，支持匹配menuCode和url，用于顶栏菜单高亮
function isTopLevelMenu(current, Menus=[]) {
  for (const item of Menus) {
    if (item.url === current || item.menuCode === current) {
        return true;
    }
    if (item.childList) {
        for (const child of item.childList) {
            if (child.url === current || child.menuCode === current) {
                return true;
            }
        }
    }
  }
  return false;
}

function getItemBySuperId(superId, Menus=[]) {
  for (const item of Menus) {
      if (item.id === superId) {
          return item;
      }
      if (item.childList) {
          for (const child of item.childList) {
              if (child.id === superId) {
                  return child;
              }
          }
      }
  }
  return null;
}

// 查找二级以内的父菜单的menuCode
function findTopLevelMenu(item, appPermissions) {
  if (isTopLevelMenu(item.menuCode, appPermissions)) {
      return item.menuCode;
  } else {
      const {superId} = item;
      const superItem = getItemBySuperId(superId, appPermissions);
      return findTopLevelMenu(superItem, appPermissions);
  }
}
export {
  MenuAdditionalSettings,
  findMatchingKey,
  isTopLevelMenu,
  findMatchingItem,
  getItemBySuperId,
  findTopLevelMenu
}
